package com.orhon.smartcampus.modules.student.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.student.dao.SInformationDao;
import com.orhon.smartcampus.modules.student.entity.SInformation;
import com.orhon.smartcampus.modules.student.gqlModel.GQLStudent;
import com.orhon.smartcampus.modules.student.mm.StudentMapper;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.UserTypeUtil;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class StudentsFetcher implements DataFetcher {

	@Autowired
	SInformationDao sInformationDao;

	@Autowired
	TokenService tokenService;


	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {

		String token = tokenService.getSystemToken();

		List<GQLStudent> students = new ArrayList<>();
		GQLPageParam pageParam = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from SInformation as stu where 1=1 " + UserTypeUtil.decide(token);

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("deleted_at")!=null) {
				hql += " and stu.deleted_at  " + Util.decide(map.get("deleted_at"));
			}
			if(map.get("id")!=null) {
				hql += " and stu.id  " + Util.decide(map.get("id"));
			}
			if(map.get("number")!=null) {
				hql += " and stu.student_number " + Util.decide(map.get("number"));
			}
			if(map.get("learn_code")!=null) {
				hql += " and stu.student_learncode " + Util.decide(map.get("learn_code"));
			}
			if(map.get("name")!=null) {
				hql += " and stu.student_name" + Util.decide(map.get("name"));
			}
			if(map.get("pinyin_name")!=null) {
				hql += " and stu.pinyin_name " + Util.decide(map.get("pinyin_name"));
			}
			if(map.get("idcard")!=null) {
				hql += " and stu.idcard " +Util.decide( map.get("idcard"));
			}
			if(map.get("gender")!=null) {
				hql += " and stu.sex " +Util.decide(map.get("gender"));
			}
			if(map.get("school_id")!=null) {
				hql += " and school_id "+ Util.decide(map.get("school_id"));
			}
			if(map.get("nation")!=null) {
				hql += " and stu.nation " + Util.decide(map.get("nation"));
			}
			if(map.get("political")!=null) {
				hql += " and stu.political " + Util.decide(map.get("political"));
			}
			if(map.get("tel_number")!=null) {
				hql += " and stu.tel_number" + Util.decide(map.get("tel_number"));
			}
			if(map.get("household_registed_id")!=null) {
				hql += " and stu.household_registed_id" + Util.decide(map.get("household_registed_id"));
			}
			if(map.get("household_registed_city_id")!=null) {
				hql += " and stu.household_registed_city_id " + Util.decide(map.get("household_registed_city_id"));
			}
			if(map.get("household_registed_county_id")!=null) {
				hql += " and stu.household_registed_county_id " + Util.decide(map.get("household_registed_county_id"));
			}
			if(map.get("is_onlychild")!=null) {
				hql += " and stu.is_onlychild " + Util.decide(map.get("is_onlychild"));
			}
			if(map.get("goEclass")!=null) {
				hql += " and stu.id in (select student from EclassRecords where go_eclass = " + Integer.parseInt(map.get("goEclass").toString()) + ")";
			}
			hql += " order by id desc";
		}
		System.out.println(hql);
		List<SInformation> list = sInformationDao.find(hql , null ,pageParam.getPage() , pageParam.getLimit());

		for (SInformation sInformation : list) {
			GQLUser gqlUser = null;
			if (env.getSelectionSet().contains("data/user")){
				Users user = sInformation.getUser();
				gqlUser = UserMapper.MAPPER.mapUser(user);
			}

			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools school = sInformation.getSchool();
				gqlSchool = SchoolMapper.MAPPER.toSchool(school);
			}
			GQLStudent student = StudentMapper.MAPPER.toStudent(sInformation);
			student.setUser(gqlUser);
			student.setSchool(gqlSchool);
			students.add(student);
		}
		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = sInformationDao.getTotalCount(hql);
		}
		return Result.ok().put("data", students).put("total", total);
	}


}
